﻿#include "lovemainwindow.h"
#include "ui_lovemainwindow.h"
//初始化样式
void loveMainWindow::iniUI()
{
    addShadow(*ui->LabCurMedia);
    addShadow(*ui->LabRatio);
    addShadow(*ui->leftLabel);
    addShadow(*ui->rightLabel);
    addShadow(*ui->timeLabel);
//    QFile file(QCoreApplication::applicationDirPath() + "/ui.qss");
    QFile file(":/qss/ui.qss");
    file.open(QFile::ReadOnly);
    if(file.isOpen())
    {
        QString style=QLatin1String(file.readAll());
        ui->centralWidget->setStyleSheet(style);
        ui->LabCurMedia->setStyleSheet(style);
        ui->LabRatio->setStyleSheet(style);
        file.close();
    }
    else
    {
        qDebug("Open failed");
    }
    //设置窗体透明
    this->setAttribute(Qt::WA_TranslucentBackground, true);
    //设置无边框
//    this->setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
}
//增加ListView阴影
void loveMainWindow::addShadow(QWidget& name)
{
//    ui->outText->setStyleSheet("background: #FFFFFF;");
    QGraphicsDropShadowEffect *shadow_effect = new QGraphicsDropShadowEffect(this);
    shadow_effect->setOffset(0, 0);              //阴影的偏移量
    shadow_effect->setColor(QColor(68, 43, 43)); //阴影的颜色
    shadow_effect->setBlurRadius(20);             // 阴影圆角的大小
    name.setGraphicsEffect(shadow_effect); //给那个控件设置阴影，这里需要注意的是所有此控件的子控件，也都继承这个阴影。
}
loveMainWindow::loveMainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::loveMainWindow)
{
    ui->setupUi(this);
    ui->statusBar->hide();
    ui->menuBar->hide();
    ui->mainToolBar->hide();
    iniUI();
    player=new QMediaPlayer(this);
    player->setNotifyInterval(2000);
    player->setVideoOutput(ui->videoWidget);
    ui->videoWidget->setMediaPlayer(player);
    ui->LabCurMedia->setText("LOVE");
    player->setMedia(QUrl::fromLocalFile(QStringLiteral("./love.mp4")));
    player->play();
//    QmyVideoWidget *a=new QmyVideoWidget(this);
//    ui->videoWidget->setGeometry(this->pos().x(),this->pos().y(),ui->videoWidget->width(),ui->videoWidget->height());
//    ui->videoWidget->show();
    ui->videoWidget->setParent(this);
    ui->videoWidget->show();
    connect(player,SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(onStateChanged(QMediaPlayer::State)));
    connect(player,SIGNAL(positionChanged(qint64)),this,SLOT(onPositionChanged(qint64)));
    connect(player,SIGNAL(durationChanged(qint64)),this,SLOT(onDurationChanged(qint64)));
}

loveMainWindow::~loveMainWindow()
{
    delete ui;
}
void loveMainWindow::onStateChanged(QMediaPlayer::State state)
{
    ui->btnPlay->setEnabled(!(state==QMediaPlayer::PlayingState));
    ui->btnPause->setEnabled(state==QMediaPlayer::PlayingState);
    ui->btnStop->setEnabled(state==QMediaPlayer::PlayingState);
}
void loveMainWindow::onDurationChanged(qint64 duration)
{
    ui->sliderPosition->setMaximum(duration);
    int secs=duration/1000;//秒
    int mins=secs/60;//分
    secs=secs%60;//余数秒
    durationTime=QString::asprintf("%d:%d",mins,secs);
    ui->LabRatio->setText(positionTime+"/"+durationTime);
}
void loveMainWindow::onPositionChanged(qint64 position)
{
    if(ui->sliderPosition->isSliderDown())
        return;
    ui->sliderPosition->setSliderPosition(position);
    int secs=position/1000;
    int mins=secs/60;
    secs=secs%60;
    positionTime=QString::asprintf("%d:%d",mins,secs);
    ui->LabRatio->setText(positionTime+"/"+durationTime);
}

void loveMainWindow::on_btnAdd_clicked()
{
    QString curPath=QDir::homePath();
    QString dlgTitle="选择视频文件";
    QString filter="mp4文件(*.mp4);;wmv文件(*.wmv);;所有文件(*.*)";
    QString aFile=QFileDialog::getOpenFileName(this,dlgTitle,curPath,filter);
    if(aFile.isEmpty())
    {
        return;
    }
    QFileInfo fileInfo(aFile);
    ui->LabCurMedia->setText(fileInfo.fileName());
    player->setMedia(QUrl::fromLocalFile(aFile));
    player->play();
    ui->videoWidget->show();
}

void loveMainWindow::on_btnPlay_clicked()
{
    player->play();
}

void loveMainWindow::on_btnPause_clicked()
{
    player->pause();
}

void loveMainWindow::on_btnStop_clicked()
{
    player->stop();
}
void loveMainWindow::on_pushButton_clicked()
{
    ui->videoWidget->setFullScreen(true);

}

void loveMainWindow::on_btnSound_clicked()
{
    bool mute=player->isMuted();
    player->setMuted(!mute);
    if(mute)
        ui->btnSound->setIcon(QIcon(":/ico/niu.ico"));
    else
        ui->btnSound->setIcon(QIcon(":/ico/pig.ico"));
}

void loveMainWindow::on_sliderVolumn_valueChanged(int value)
{
    player->setVolume(value);
}
